package net.jayson.flutter_ezviz.download;

import io.flutter.plugin.common.MethodChannel.Result;

import android.content.Context;
import android.os.Environment;
import android.os.Handler;
import android.src.main.kotlin.net.jayson.flutter_ezviz.download.XCloudPartInfoFile;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.videogo.openapi.EZOpenSDKListener;
import com.videogo.openapi.bean.EZCloudRecordFile;
import com.videogo.stream.EZCloudStreamDownload;
import com.videogo.util.Utils;


import java.io.File;

/**
 * EZCloudDownloadHelper
 *
 * @author xiaohao
 * @email 18728888305@163.com
 * @time 2021/8/29
 */
public class EZCloudDownloadHelper {

    private static Handler handler = new Handler();

    /**
     * 下载云录像
     *
     * @param cloudFileStr 云录像的json对象
     * @param context      上下文
     * @param verifyCode   密码
     */
    public static void startDownloadCloudVideo(String cloudFileStr, final Context context, final String verifyCode, final Result result) {
        if (cloudFileStr == null) {
            return;
        }
        final XCloudPartInfoFile cloudFile = new Gson().fromJson(cloudFileStr, new TypeToken<XCloudPartInfoFile>() {
        }.getType());
        new TaskManager().submit(new Runnable() {
            @Override
            public void run() {
//                final String strFileNameWithPath = context.getExternalCacheDir() + "/0_OpenSDK" + "/cloud_" + System.currentTimeMillis() + ".mp4";
                final String strFileNameWithPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + "/cloud_" + System.currentTimeMillis() + ".mp4";
                final File file = new File(strFileNameWithPath);
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }
                EZCloudRecordFile eZCloud = new EZCloudRecordFile();
                convertCloudPartInfoFile2EZCloudRecordFile(eZCloud, cloudFile);
                final EZCloudStreamDownload ezCloudStreamDownloader = new EZCloudStreamDownload(strFileNameWithPath, eZCloud);
                ezCloudStreamDownloader.setStreamDownloadCallback(new EZOpenSDKListener.EZStreamDownloadCallback() {

                    @Override
                    public void onSuccess(String s) {
                        Log.e("startDownloadCloudVideo", s);
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                result.success("下载成功:" + strFileNameWithPath);
                            }
                        });
                    }

                    @Override
                    public void onError(final EZOpenSDKListener.EZStreamDownloadError ezStreamDownloadError) {
                        Log.e("startDownloadCloudVideo", ezStreamDownloadError.toString());
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    result.success("下载失败:" + ezStreamDownloadError.toString());
                                }catch (Exception e){
                                    Log.e("startDownloadCloudVideo", e.toString());
                                }
                            }
                        });
                    }
                });
                ezCloudStreamDownloader.setSecretKey(verifyCode);
                ezCloudStreamDownloader.start();
            }
        });
    }


    private static void convertCloudPartInfoFile2EZCloudRecordFile(EZCloudRecordFile dst, XCloudPartInfoFile src) {
        dst.setCoverPic(src.getCoverPic());
        dst.setDownloadPath(src.getDownloadPath());
        dst.setFileId(src.getFile_id());
        dst.setEncryption(src.getKey_checksum());
        dst.setStartTime(Utils.convert14Calender(src.getStart_time()));
        dst.setStopTime(Utils.convert14Calender(src.getStop_time()));
        dst.setDeviceSerial(src.getDev_serial());
        dst.setCameraNo(Integer.parseInt(src.getChannel_no()));
        dst.setVideoType(src.getVideoType());
        dst.setiStorageVersion(src.getIstorageVersion());
    }
}
